import "babel-polyfill"; //兼容IE浏览器
import Vue from 'vue'
import App from './App.vue'
//引入elementUI
import ElementUI from 'element-ui'
//element UI版本号为1.4.13
import 'element-ui/lib/theme-default/index.css'
//引入VueRouter
import VueRouter from 'vue-router'
import routes from './routes'
//引入VueResource
import VueResource from 'vue-resource'
//引入字符图标
import '../icon/iconfont.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(VueResource)

//时间过滤器
Vue.filter('moment', function (value, formatString) {
	var moment = require('moment');
    formatString = formatString || 'YYYY-MM-DD';
    return moment(value).format(formatString);
});
//包厢状态过滤器
Vue.filter('bookings', function (value) {
	var val = value || []
	var temp = []
	val.forEach(function (item) {
		var flag = false
		temp.forEach(function (t) {
			if (item === t) {
				flag = true
			}
        })

		if (!flag) {
			temp.push(item)
		}
    })

	console.log(temp)

	return temp
});

//创建一个router实例
const router = new VueRouter({
	routes
})

router.beforeEach((to, from, next) => {
	if (to.path === '/login') {
    	sessionStorage.removeItem('user');
	}

	let user = JSON.parse(sessionStorage.getItem('user'));
	if (!user && to.path !== '/login') {
    	next({ path: '/login' })
	} else {
    	next()
	}
})

new Vue({
  router,
  methods : {
  	//判断菜单是否展示
	fnShowMenu(data){
		var routes = this.$router.options.routes
		//当前用户为审核人
		if(data.IsApprover) {
			for(var i = 0; i < routes.length; i++) {
				var item = routes[i]
				if(item.iconCls ==="el-icon-custom-managerment"
					|| item.iconCls ==="el-icon-custom-apply"
					|| item.iconCls ==="el-icon-custom-my-managerment"
					|| item.iconCls ==="el-icon-custom-my-apply") {
					item.hidden = false
					var child = item.children || []
					for(var j = 0; j < child.length; j++) {
						child[j].hidden = false
					}
				}
			}
		}

		//当前用户为管理员
		if(data.IsAdmin) {
			for(var i = 0; i < routes.length; i++) {
				var item = routes[i]
				if(item.path === "/home" && item.iconCls !== "el-icon-custom-managerment2") {
					item.hidden = false
					var child = item.children || []
					for(var j = 0; j < child.length; j++) {
						child[j].hidden = false
					}
				}
			}
		}

		//当前用户为后勤人员
		if(data.IsLogistics) {
			for(var i = 0; i < routes.length; i++) {
				var item = routes[i]
				if(item.iconCls ==="el-icon-custom-managerment2"
					|| item.iconCls ==="el-icon-custom-apply"
					|| item.iconCls ==="el-icon-custom-my-apply"
					|| item.iconCls ==="el-icon-custom-search"
					|| item.iconCls === "el-icon-custom-report") {
					item.hidden = false
					var child = item.children || []
					for(var j = 0; j < child.length; j++) {
						child[j].hidden = false
					}
				}
			}
		}

		//当前用户非管理员、非审核人
		if (!data.IsAdmin && !data.IsApprover && !data.IsLogistics) {
			for(var i = 0; i < routes.length; i++) {
				var item = routes[i]
				if(item.iconCls ==="el-icon-custom-apply" || item.iconCls ==="el-icon-custom-my-apply") {
					item.hidden = false
					var child = item.children || []
					for(var j = 0; j < child.length; j++) {
						child[j].hidden = false
					}
				}
			}
		}
	}
  },
  mounted() {
  	let user = JSON.parse(sessionStorage.getItem('user'))
	if (user) { this.fnShowMenu(user) }
  },
  render: h => h(App)
}).$mount('#app')
